﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.IO;
using System.Reflection;
using ExtremeML.Packaging;
using NUnit.Framework;

namespace ExtremeML.Tests
{
	[TestFixture]
	public class StreamedOutput
	{
		[Test]
		public void VerifyStreamedPackageCreation()
		{
			var path = Utility.GetTemporaryFilePath();

			using (var output = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
			using (var package = SpreadsheetDocumentWrapper.Create(output))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.AreEqual(0, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
			}

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.AreEqual(0, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
			}
		}

		[Test]
		public void VerifyStreamedTemplateOutput()
		{
			var path = Utility.GetTemporaryFilePath();

			using (var input = Assembly.GetExecutingAssembly().GetManifestResourceStream(Utility.EmptyWorkbookTemplateResource))
			using (var output = new FileStream(path, FileMode.Create, FileAccess.Write))
			using (var package = SpreadsheetDocumentWrapper.Open(input, output))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.AreEqual(3, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
			}

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.AreEqual(3, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
			}
		}

		[Test]
		public void VerifyReOpeningPackageAccessedByPath()
		{
			var path = Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource);

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				var table = package.WorkbookPart.GetTablePart("PopulatedNoTotals").Table;
				table.Extend(5);
			}

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				var table = package.WorkbookPart.GetTablePart("PopulatedNoTotals").Table;

				Assert.AreEqual(9, table.DataRowSpan, "Table data rows");
			}
		}

	}
}
